package com.miguo.db;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;

import de.greenrobot.dao.AbstractDao;
import de.greenrobot.dao.Property;
import de.greenrobot.dao.internal.DaoConfig;

import com.miguo.bean.CategoryBean;

public class CategoryDao extends AbstractDao<CategoryBean, Void> {

    public static final String TABLENAME = "category";

    /**
     * Properties of entity category.<br/>
     * Can be used for QueryBuilder and for referencing column names.
     */
    public static class Properties {
        public final static Property Id = new Property(0, Long.class, "id", false, "id");
        public final static Property Name = new Property(1, String.class, "name", false, "name");
        public final static Property Icon = new Property(2, String.class, "icon", false, "icon");
        public final static Property Pid = new Property(3, Long.class, "pid", false, "pid");
        public final static Property Ordid = new Property(4, Long.class, "ordid", false, "ordid");
    }

    ;


    public CategoryDao(DaoConfig config) {
        super(config);
    }

    public CategoryDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
    }

    /**
     * Creates the underlying database table.
     */
    public static void createTable(SQLiteDatabase db, boolean ifNotExists) {
        String constraint = ifNotExists ? "IF NOT EXISTS " : "";
        db.execSQL("CREATE TABLE " + constraint + "'category' (" + //
                "'id' INTEGER," + // 0: id
                "'name' TEXT," + // 1: name
                "'icon' TEXT," + // 2: icon
                "'pid' INTEGER," + // 3: pid
                "'ordid' INTEGER);"); // 4: ordid
    }

    /**
     * Drops the underlying database table.
     */
    public static void dropTable(SQLiteDatabase db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "'category'";
        db.execSQL(sql);
    }

    /**
     * @inheritdoc
     */
    @Override
    protected void bindValues(SQLiteStatement stmt, CategoryBean entity) {
        stmt.clearBindings();

        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }

        String name = entity.getName();
        if (name != null) {
            stmt.bindString(2, name);
        }

        String icon = entity.getIcon();
        if (icon != null) {
            stmt.bindString(3, icon);
        }

        Long pid = entity.getPid();
        if (pid != null) {
            stmt.bindLong(4, pid);
        }

        Long ordid = entity.getOrdid();
        if (ordid != null) {
            stmt.bindLong(5, ordid);
        }
    }

    /**
     * @inheritdoc
     */
    @Override
    public Void readKey(Cursor cursor, int offset) {
        return null;
    }

    /**
     * @inheritdoc
     */
    @Override
    public CategoryBean readEntity(Cursor cursor, int offset) {
        CategoryBean entity = new CategoryBean( //
                cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
                cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1), // name
                cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // icon
                cursor.isNull(offset + 3) ? null : cursor.getLong(offset + 3), // pid
                cursor.isNull(offset + 4) ? null : cursor.getLong(offset + 4) // ordid
        );
        return entity;
    }

    /**
     * @inheritdoc
     */
    @Override
    public void readEntity(Cursor cursor, CategoryBean entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setName(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1));
        entity.setIcon(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2));
        entity.setPid(cursor.isNull(offset + 3) ? null : cursor.getLong(offset + 3));
        entity.setOrdid(cursor.isNull(offset + 4) ? null : cursor.getLong(offset + 4));
    }

    /**
     * @inheritdoc
     */
    @Override
    protected Void updateKeyAfterInsert(CategoryBean entity, long rowId) {
        // Unsupported or missing PK type
        return null;
    }

    /**
     * @inheritdoc
     */
    @Override
    public Void getKey(CategoryBean entity) {
        return null;
    }

    /**
     * @inheritdoc
     */
    @Override
    protected boolean isEntityUpdateable() {
        return true;
    }

}